function e(e, n, i) {
    return n in e ? Object.defineProperty(e, n, {
        value: i,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : e[n] = i, e;
}

wxapp.tools.BuildDownloadFile = function(e, n) {
    e.file = n;
    var i = n.lastIndexOf("/");
    -1 == i ? (e.name = n, e.path = "") : (e.name = n.substring(i + 1), e.path = n.substring(0, i)), 
    e.url = window.REMOTE_SERVER_ROOT + n;
}, wxapp.DownloadFiles = cc.Class.extend({
    ctor: function() {
        this.files = [];
    },
    addFile: function(e) {
        var n = {};
        n.file = e;
        var i = e.lastIndexOf("/");
        -1 == i ? (n.name = e, n.path = "") : (n.name = e.substring(i + 1), n.path = e.substring(0, i)), 
        this.files.push(n);
    },
    getFiles: function() {
        return this.files;
    }
}), wxapp.Downloader = cc.Class.extend({
    ctor: function() {
        this.downloadFailNum = 0, this.downloadFailedFiles = [];
    },
    downloadImageByUrl: function(n, i, s, t, a) {
        var c;
        void 0 == a ? a = "/selfDefine/" : wxapp.wxFileManager.hasPath(a) || wxapp.wxFileManager.createdir(a), 
        "/" != a[a.length - 1] && (a += "/");
        var o = s, l = t, r = wx.env.USER_DATA_PATH + "/" + a + i;
        wxapp.wxFileManager.accessFile((c = {
            filePath: r
        }, e(c, "filePath", r), e(c, "sucess", function(e) {
            e.path = r, o && o(e);
        }), e(c, "fail", function(e) {
            wx.downloadFile({
                url: n,
                success: function(e) {
                    wxapp.wxFileManager.copyFile({
                        srcPath: e.tempFilePath,
                        destPath: r,
                        success: function(e) {
                            e.path = r, o && o(e);
                        },
                        fail: function(e) {
                            l && l(e);
                        }
                    });
                },
                fail: function(e) {
                    l && l(e);
                },
                complete: function(e) {}
            });
        }), c));
    },
    downloadSaveFile: function(e) {
        var n = e.success, i = e.fail, s = window.REMOTE_SERVER_ROOT + e.url, t = wx.env.USER_DATA_PATH + s.replace(window.REMOTE_SERVER_ROOT, "/");
        wx.downloadFile({
            url: s,
            filePath: t,
            success: function(e) {
                n(e);
            },
            fail: function(s) {
                wxapp.wxDownLoader.downloadFailNum++, wxapp.wxDownLoader.downloadFailNum > 10 ? (wxapp.wxDownLoader.downloadFailedFiles.push(e.url), 
                n(s)) : wxapp.wxDownLoader.downloadSaveFile({
                    url: e.url,
                    success: n,
                    fail: i
                });
            }
        });
    },
    downloadSaveFiles: function(e) {
        var n = e.success, i = e.fail, s = e.urls, t = s.length, a = e.onProgress, c = 0;
        wxapp.wxDownLoader.downloadFailedFiles = [];
        for (var o = 0; o < t; o++) wxapp.wxDownLoader.downloadSaveFile({
            url: s[o],
            success: function(e) {
                if ((c += 1) == t) n && n(c); else {
                    var i = c / t * 100;
                    a(parseInt(i));
                }
            },
            fail: function(e) {
                i && i(e);
            }
        });
    }
}), wxapp.WxFileManager = cc.Class.extend({
    ctor: function() {
        this.FileSystemManager = null;
    },
    createGameResourceDir: function() {
        window.wx && (this.accessPath(wx.env.USER_DATA_PATH + "/res"), this.accessPath(wx.env.USER_DATA_PATH + "/res/games"), 
        this.accessPath(wx.env.USER_DATA_PATH + "/res/games/newfish"), this.accessPath(wx.env.USER_DATA_PATH + "/res/games/newfish/config"), 
        this.accessPath(wx.env.USER_DATA_PATH + "/res/games/newfish/config_src"), this.accessPath(wx.env.USER_DATA_PATH + "/res/games/newfish/csb"), 
        this.accessPath(wx.env.USER_DATA_PATH + "/res/games/newfish/img"), this.accessPath(wx.env.USER_DATA_PATH + "/res/games/newfish/particle"), 
        this.accessPath(wx.env.USER_DATA_PATH + "/res/games/newfish/sound"), this.accessPath(wx.env.USER_DATA_PATH + "/res/games/newfish/csb"), 
        this.accessPath(wx.env.USER_DATA_PATH + "/res/games/newfish/csb/lobby_common"));
    },
    getInstance: function() {
        return window.wx && null == this.FileSystemManager && (this.FileSystemManager = wx.getFileSystemManager()), 
        this.FileSystemManager;
    },
    accessPath: function(e) {
        window.wx && (e.indexOf("."), this.getInstance().access({
            path: e,
            success: function(e) {},
            fail: function(n) {
                wxapp.wxFileManager.mkdir(e);
            },
            complete: function(e) {}
        }));
    },
    accessFile: function(e, n) {
        if (window.wx) {
            if (void 0 == n && (n = !0), e.filePath.indexOf(".") < 0 && n) return;
            var i = e.filePath, s = e.sucess, t = e.fail;
            this.getInstance().access({
                path: i,
                success: function(e) {
                    s(e);
                },
                fail: function(e) {
                    t(e);
                },
                complete: function(e) {}
            });
        }
    },
    mkdir: function(e) {
        window.wx && this.getInstance().mkdir({
            dirPath: e,
            success: function(e) {},
            fail: function(e) {},
            complete: function(e) {}
        });
    },
    hasPath: function(e) {
        if (!window.wx) return console.log("err h5 not support hasPath :" + e), !1;
        if (0 === e.length) return !0;
        var n = e.indexOf("."), i = e.lastIndexOf("/");
        if (-1 !== n) {
            if (-1 === i) return !0;
            e = e.substring(0, i);
        }
        var s = wx.env.USER_DATA_PATH + "/" + e, t = wx.getFileSystemManager();
        try {
            return t.accessSync(s), !0;
        } catch (e) {
            return console.log("no such dic :" + s), !1;
        }
    },
    createdir: function(e) {
        if (window.wx) {
            if (0 === e.length) return !0;
            var n = e.lastIndexOf("/");
            if (-1 !== e.indexOf(".")) {
                if (-1 === n) return !0;
                e = e.substring(0, n);
            }
            for (var i = this.getInstance(), s = e.split("/"), t = wx.env.USER_DATA_PATH, a = !0, c = 0; c < s.length; c++) {
                t += "/" + s[c];
                try {
                    i.accessSync(t);
                } catch (e) {
                    console.log("no such dic , then make:" + t);
                    try {
                        i.mkdirSync(t), console.log("make success:" + t);
                    } catch (e) {
                        a = !1, console.log("make fail:" + t);
                    }
                }
            }
            return a;
        }
        return console.log("err h5 not support mkdirs :" + e), !1;
    },
    copyFile: function(e) {
        window.wx && this.getInstance().copyFile({
            srcPath: e.srcPath,
            destPath: e.destPath,
            success: e.success,
            fail: e.fail,
            complete: function(e) {}
        });
    },
    getSavedFileList: function(e) {
        window.wx && this.getInstance().getSavedFileList({
            success: e.success,
            complete: e.complete,
            fail: e.fail
        });
    },
    getPathFileList: function(e) {
        window.wx && this.getInstance().readdir({
            dirPath: e.dirPath,
            success: e.success,
            complete: function(e) {},
            fail: e.fail
        });
    },
    outPutPathFileList: function(e) {
        window.wx && this.getInstance().readdir({
            dirPath: e,
            success: function(n) {
                for (var i = n.files, s = 0; s < i.length; s++) cc.log(e + " index:" + s + " = " + i[s]);
            },
            complete: function(e) {},
            fail: function(e) {
                cc.log("log filepath failede:" + e.errMsg);
            }
        });
    },
    unzipFile: function(e) {
        window.wx && this.getInstance().unzip({
            zipFilePath: e.zipFilePath,
            targetPath: e.targetPath,
            success: e.success,
            complete: function(e) {},
            fail: e.fail
        });
    },
    getFileMd5: function(e, n, i) {
        window.wx ? wx.getFileInfo({
            filePath: e,
            digestAlgorithm: "md5",
            success: function(e) {
                var s = e.digest ? e.digest : "";
                n.call(i, s);
            },
            fail: function(e) {
                n.call(i, "");
            }
        }) : (cc.log("md5值计算目前仅支持 微信环境"), n.call(i, ""));
    },
    deleteFile: function(e) {
        if (window.wx) {
            var n = e.success, i = e.fail;
            wxapp.wxFileManager.accessFile({
                filePath: e.dirPath,
                sucess: function(s) {
                    wxapp.wxFileManager.getInstance().unlink({
                        filePath: e.filePath,
                        success: n,
                        complete: function(e) {},
                        fail: i
                    });
                },
                fail: function(e) {
                    n();
                }
            });
        }
    },
    deleteDir: function(e) {
        window.wx && wxapp.wxFileManager.accessFile({
            filePath: e.dirPath,
            sucess: function(n) {
                wxapp.wxFileManager.getInstance().rmdir({
                    dirPath: e.dirPath,
                    success: e.success,
                    complete: function(e) {},
                    fail: e.fail
                });
            },
            fail: function(n) {
                e.success(n);
            }
        }, !1);
    },
    setStorage: function(e, n) {
        window.wx && wx.setStorageSync(e, n);
    },
    getStorage: function(e) {
        return window.wx ? wx.getStorageSync(e) : null;
    }
});